#!/usr/local/bin/python
# -*- coding: utf-8 -*-


import initIncomeStatementPage
from data_alchemy.account_book_alchemy import AccountBookAlchemy
from data_alchemy.income_statement_alchemy import IncomeStatementAlchemy
from data_alchemy.income_statement_monthly_alchemy import IncomeStatementMonthlyAlchemy


class IncomeStatement:
    '''
    类功能：收支表的计算
    '''

    def __init__(self, year=2022, month=1):
        '''
        函数功能：
        '''

        self.year = year
        self.month = month
        self.accountBookDetail = {}     #暂时未使用
        self.propertyIncrease = {}      #当月各科目资产增加情况
        self.propertyDecrease = {}      #当月各科目资产减少情况

    def AddIncomeStatementGroupInfor(self, year, month, subjectName, subjectPath):
        '''
        函数功能：添加一组收支表数据
        :return:
        '''

        accountBookAlchemy = AccountBookAlchemy()
        result = accountBookAlchemy.SearchAccountBookByMonthSub(year, month, subjectPath)

        if len(result)==0:
            return False
        currentMonthValue=0.0
        totalValue=0.0

        currentMonthTotal = 0.0
        accumulateTotal = 0.0

        for i in range(len(result)):
            dataRow = result[i]
            if (dataRow.decrease_value==0 ):
                #类型：收入类型
                currentMonthValue = dataRow.increase_value
            elif (dataRow.increase_value==0 ):
                #类型：支出类型
                currentMonthValue = dataRow.decrease_value

            totalValue = dataRow.end_value
            if (totalValue<0):
                totalValue=totalValue*(-1)

            incomeStatementAlchemy = IncomeStatementAlchemy(year=self.year,
                                                            month=self.month,
                                                            subjectName=dataRow.subject_name,
                                                            currentMonthValue=currentMonthValue,
                                                            totalValue=totalValue
                                                            )

            incomeStatementAlchemy.AddIncomeStatementItem()
            currentMonthTotal = currentMonthTotal + currentMonthValue
            accumulateTotal = accumulateTotal + totalValue



        incomeStatementAlchemy = IncomeStatementMonthlyAlchemy(year=self.year,
                                                               month=self.month,
                                                               dataType=subjectName,
                                                               currentMonthValue=currentMonthTotal,
                                                               totalValue=accumulateTotal
                                                               )
        incomeStatementAlchemy.AddIncomeStatementItem()

        return True

    def genIncomeStatement(self):
        '''
        函数功能：生成收支表
        :return:
        '''

        incomeStatementAlchemy = IncomeStatementAlchemy()
        incomeStatementAlchemy.DelIncomeStatementByMonth(self.year, self.month)

        incomeStatementMonthlyAlchemy = IncomeStatementMonthlyAlchemy()
        incomeStatementMonthlyAlchemy.DelIncomeStatementByMonth(self.year, self.month)


        for subjectName in initIncomeStatementPage.INCOME_STATEMENT_LIST:
            subjectPath = initIncomeStatementPage.INCOME_STATEMENT_LIST[subjectName]

            self.AddIncomeStatementGroupInfor(self.year, self.month, subjectName, subjectPath)



    def getIncomeStatementDataMonthly(self, subjectPath):
        '''
        函数功能；获取收支表数据
        :return:
        '''

        incomeStatementAlchemy = IncomeStatementAlchemy()
        incomeStatementResult = incomeStatementAlchemy.SearchIncomeStatementByMonthSub(self.year, self.month, subjectPath)
        return  incomeStatementResult

def main():
    incomeStatement = IncomeStatement(2022, 7)
    incomeStatement.genIncomeStatement()
    pass


if __name__ == '__main__':
    main()